import Mock from 'mockjs'
import { getQueryParameters } from '../util'

const userList = []
const userCount = 20

for (let i = 0; i < userCount; i++) {
  userList.push(Mock.mock({
    token: '@guid',
    userInfo: {
      id: '@id',
      name: '@cname',
      username: '@first',
      email: '@email',
      phone: /^1[3456789]\d{9}$/,
      'role|1': ['admin', 'user'],
      'status|1': ['active', 'disabled'],
      createTime: '@datetime',
      avatar: '@image("200x200")'
    },
  }))
}

export default {
  // 登录
  'POST /api/user/login': (req) => {
    const { username, password } = JSON.parse(req.body)
    if (username === 'admin' && password === '123456') {
      return {
        code: 200,
        data: {
          token: Mock.Random.uuid(),
          user: userList[0]
        },
        message: '登录成功'
      }
    }
    return {
      code: 403,
      message: '用户名或密码错误'
    }
  },

  // 获取用户信息
  'GET /api/user/info': {
    code: 200,
    data: userList[0],
    message: '获取成功'
  },

  // 获取用户列表
  'GET /api/users': (req) => {
    const params = getQueryParameters(req)
    const { page = 1, pageSize = 10 } = params
    
    const start = (page - 1) * pageSize
    const end = start + pageSize
    const list = userList.slice(start, end)

    return {
      code: 200,
      data: {
        list,
        pagination: {
          total: userList.length,
          page: parseInt(page),
          pageSize: parseInt(pageSize)
        }
      },
      message: '获取成功'
    }
  }
}